<?php

  define('BASE_ROOT_PATH', dirname(dirname(__FILE__)));
	include_once( BASE_ROOT_PATH . "/word/db/DBMySQL.php");  

	$memberId = $_REQUEST['id'];
	if(empty($memberId)) {
	  $msg = json_encode("用户id不能为空");
		echo '{"code":"1","msg":'.$msg.'}';
		exit();
	}
	
	$pageNo = $_REQUEST['pageNo'];
	if(empty($pageNo)) {
	  $pageNo = '';
	}			
	
	$pageSize = $_REQUEST['pageSize'];
	if(empty($pageSize)) {
	  $pageSize = 10;
	}		
	
	$notme = $_REQUEST['notme'];
	if(empty($notme)) {
	  $notme = 0;
	}			
	
	// 查找用户
	$db = new DBMySQL();
	$rs = $db->Query("select m.member_id from t_member m where m.member_id = $memberId limit 1");
	if(empty($rs)) {
	  $msg = json_encode("该用户不存在");
		echo '{"code":"1","msg":'.$msg.'}';
		exit();
	}
	
	$to = $_REQUEST['to'];
	$timestamp = $_REQUEST['timestamp'];
	if(empty($timestamp)) {
	  $timestamp = time()-365 * 24 * 60 * 60;
	}
	$total = 0;	
	if(!empty($to)) { // to 非空,只查询本用户和1个指定用户的消息
	  
	  if(1 == $notme) { // 不包含自己发送的信息
	    $rs = $db->Query("select count(m.msg_id) as total from t_msg m where m.from_id = $to and m.to_id = $memberId and  m.create_time > '$timestamp' ");
	    $total = $rs[0]['total'];
	    if(0 < $total) {
	      $sql = "select m.create_time,m.from_id,m.to_id,m.content from t_msg m where m.from_id = $to and m.to_id = $memberId and m.create_time > '$timestamp' order by m.msg_id desc ";
	      if(!empty($pageNo)) { // 指定页数
	      	$start = $pageSize * ($pageNo - 1);
	      	$sql .= " limit $start, $pageSize ";
	      } 
	    	$rs = $db->Query($sql);	    	
	    }
	  } else { // 包含自己发送的消息
	    $rs = $db->Query("select count(m.msg_id) as total from t_msg m where (m.from_id = $to and m.to_id = $memberId or m.from_id = $memberId and m.to_id = $to) and m.create_time > '$timestamp' ");
	    $total = $rs[0]['total'];
	    if(0 < $total) {
	      $sql = "select m.create_time,m.from_id,m.to_id,m.content from t_msg m where (m.from_id = $to and m.to_id = $memberId or m.from_id = $memberId and m.to_id = $to) and m.create_time > '$timestamp' order by m.msg_id desc ";
	      if(!empty($pageNo)) { // 指定页数
	      	$start = $pageSize * ($pageNo - 1);
	      	$sql .= " limit $start, $pageSize ";
	      } 
	    	$rs = $db->Query($sql);	    	
	    }	  	
	  }	  
	  
	} else { // to为空,查询所有和本用户交互的消息
	  if(1 == $notme) { // 不包含自己发送的信息
	    $rs = $db->Query("select count(m.msg_id) as total from t_msg m where m.to_id = $memberId and m.create_time > '$timestamp' ");
	    $total = $rs[0]['total'];
	    if(0 < $total) {
	      $sql = "select m.create_time,m.from_id,m.to_id,m.content from t_msg m where m.to_id = $memberId and m.create_time > '$timestamp' order by m.msg_id desc ";
	      if(!empty($pageNo)) { // 指定页数
	      	$start = $pageSize * ($pageNo - 1);
	      	$sql .= " limit $start, $pageSize ";
	      } 
	    	$rs = $db->Query($sql);	    	
	    }
	  }	else { // 包含自己发送的消息
	    $rs = $db->Query("select count(m.msg_id) as total from t_msg m where (m.to_id = $memberId or m.from_id = $memberId) and m.create_time > '$timestamp' ");
	    $total = $rs[0]['total'];
	    if(0 < $total) {
	      $sql = "select m.create_time,m.from_id,m.to_id,m.content from t_msg m where (m.to_id = $memberId or m.from_id = $memberId) and m.create_time > '$timestamp' order by m.msg_id desc ";
	      if(!empty($pageNo)) { // 指定页数
	      	$start = $pageSize * ($pageNo - 1);
	      	$sql .= " limit $start, $pageSize ";
	      } 
	    	$rs = $db->Query($sql);	    	
	    }	  	
	  }		
	}
	
	$dataArray = array();
	if(0 < $total) {	  
  	$nSize = count($rs);
  	$userInfos = array();
  	$dialogs = array();
  	for($i=0;$i<$nSize;$i++) {
  	  	$val = $rs[$i];
  	  
	    	$fromId = $val['from_id'];
	    	$toId = $val['to_id'];
	    	$content = $val['content'];
	    	$createTime = $val['create_time'];
	    	
	    	$cmpId = $fromId;
	    	if($fromId == $memberId) {
	    		$cmpId = $toId;
	    	}
    	
  		$conversation = $userInfos["$cmpId"];  		
  		if(empty($conversation)) {
  			$rsUserInfo = $db->Query("select m.email,m.nick,mi.avater from t_member m left join t_member_info mi on mi.member_id = m.member_id where m.member_id = $cmpId limit 1");
  			$userInfos["$cmpId"] = array('email' =>$rsUserInfo[0]['email'], 'nick' =>$rsUserInfo[0]['nick'], 'avater' =>$rsUserInfo[0]['avater']);
  			$dialogs["$cmpId"] = array();
  		}
  		array_push($dialogs["$cmpId"], array("$createTime" => array('sender' =>$fromId, 'content' =>$content)));
  	}
  	
		foreach ($userInfos as $key => $row) {
 			$dataArray[$key] = array(
  					'info' =>$userInfos[$key],
  					'dialog' =>$dialogs[$key]
  				);			
		}	
	
	}
	
  $data = json_encode($dataArray);
	
	echo '{"code":"0","msg":"","pageNo":"'.$pageNo.'","pageSize":"'.$pageSize.'","total":"'.$total.'","data":'.$data.'}';
	
?>